﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.UserGroup
{
	/// <summary>
	/// 用户组控制类
	/// </summary>
	/// <![CDATA[2014-7-30 11:36:48]]>
	public class Component : DLL.ComponentBase
	{
		#region 属性
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-7-30 11:36:48]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-7-30 11:36:48]]>
		private Config clsConfig = new Config();		// 配置类对象
		private Column.Component clsColumnComp;		// 栏目控制类
		private WebColumn.Component clsWebComp;		// 前台栏目控制类
		#endregion 属性

		#region 构造函数
		/// <summary>
		/// 用户组控制类构造函数
		/// </summary>
		/// <![CDATA[2014-7-30 11:36:48]]>
		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);
			clsColumnComp = new Column.Component(page);
			clsWebComp = new WebColumn.Component(page);

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strUserGroupName", clsDatabase.USERGROUPNAME, clsDatabase.listStructure[clsDatabase.USERGROUPNAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strColumnIDs", clsDatabase.COLUMNIDS, clsDatabase.listStructure[clsDatabase.COLUMNIDS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strWebColumnIDs", clsDatabase.WEBCOLUMNIDS, clsDatabase.listStructure[clsDatabase.WEBCOLUMNIDS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nQueryRight", clsDatabase.QUERYRIGHT, clsDatabase.listStructure[clsDatabase.QUERYRIGHT].strName));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strUserGroupName", clsDatabase.listStructure[clsDatabase.USERGROUPNAME].strName, "LIKE"));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strStrictUserGroupName", clsDatabase.listStructure[clsDatabase.USERGROUPNAME].strName, "="));
		}
		#endregion 构造函数

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-7-30 11:36:48]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		protected override TonyNet4V1.EntityBase DataToEntity(System.Data.DataRow dr)
		{
			Entity clsUserGroup = new Entity();

			clsUserGroup = (Entity) base.DataToEntity(dr);

			clsUserGroup.ColumnNames = clsColumnComp.QueryColumnNamesByColumnIDs(clsUserGroup.ColumnIDs);
			clsUserGroup.WebColumnNames = clsWebComp.QueryColumnNamesByColumnIDs(clsUserGroup.WebColumnIDs);

			return clsUserGroup;
		}
		/// <summary>
		/// 添加
		/// </summary>
		/// <![CDATA[2014-8-10 16:20:57]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Add(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsUserGroup = new Entity();

			clsUserGroup = (Entity)clsEntity;
			if (HaveUserGroupName(clsUserGroup.UserGroupName))
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(page, "用户组已经存在！");
				return false;
			}
			else
			{
				return base.Add(clsEntity);
			}
		}
		/// <summary>
		/// 修改
		/// </summary>
		/// <![CDATA[2014-8-10 16:24:00]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Modify(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsNewUserGroup = new Entity();
			Entity clsOldUserGroup = new Entity();

			clsNewUserGroup = (Entity)clsEntity;
			clsOldUserGroup = GetOne(((Entity)clsEntity).ID);

			if (clsNewUserGroup.UserGroupName == clsOldUserGroup.UserGroupName)
			{
				return base.Modify(clsEntity);
			}
			else
			{
				if (HaveUserGroupName(clsNewUserGroup.UserGroupName))
				{
					TonyNet4V1.ShowMessageAlert.ShowMessage(page, "用户组已经存在！");
					return false;
				}
				else
				{
					return base.Modify(clsEntity);
				}
			}
			
		}

		#region 查询
		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-7-30 11:36:48]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-7-30 11:36:48]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}
		#endregion 查询

		/// <summary>
		/// 检查用户组名是否已经存在，存在返回true，否则返回false
		/// </summary>
		/// <![CDATA[2014-8-10 16:00:07]]>
		/// <param name="strUserGroupName"></param>
		/// <returns></returns>
		private bool HaveUserGroupName(string strUserGroupName)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			clsQuery.strStrictUserGroupName = strUserGroupName;
			list = ConvertListEntityType(Query(clsQuery));

			if (list.Count > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}
